package bsh.asm.acessor;

import org.objectweb.asm.MethodVisitor;

import bsh.CompilationState;
import bsh.exception.CompilationFailedException;
import bsh.meta.MetaTypeWrapper;
import bsh.meta.TypeWrapper;

/**
 * ObjectAccessor para um field do this estatico. Nao carrega o this no stack.
 * 
 * @author takeshi
 */
public class StaticFieldObjectAccessor implements ObjectAccessor {

    private final TypeWrapper declaringType;

    private final ObjectAccessor fieldAccessor;

    /**
     * C'tor
     * 
     * @param selfType o tipo do this
     * @param accessor o {@link ObjectAccessor} do field
     */
    public StaticFieldObjectAccessor(TypeWrapper selfType, ObjectAccessor accessor) {
        super();
        this.declaringType = selfType;
        this.fieldAccessor = accessor;
    }

    /**
     * {@inheritDoc}
     */
    public void loadObject(MethodVisitor visitor, CompilationState state) throws CompilationFailedException {
        state.pushStack(new MetaTypeWrapper(this.declaringType));
        this.fieldAccessor.loadObject(visitor, state);
    }

    /**
     * {@inheritDoc}
     */
    public void preStore(MethodVisitor visitor, CompilationState state) {
        state.pushStack(new MetaTypeWrapper(this.declaringType));
    }

    /**
     * {@inheritDoc}
     */
    public boolean readOnly() {
        return this.fieldAccessor.readOnly();
    }

    /**
     * {@inheritDoc}
     */
    public void storeObject(MethodVisitor visitor, CompilationState state) throws CompilationFailedException {
        this.fieldAccessor.storeObject(visitor, state);
    }

    /**
     * {@inheritDoc}
     */
    public TypeWrapper type() {
        return this.fieldAccessor.type();
    }

}
